SET SERVEROUTPUT ON SIZE 1000000;
ALTER TRIGGER GL_PERIOD_SETS_CT1 DISABLE;



DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_PERIOD_SETS';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_PERIOD_SETS;
		
	CURSOR Cur_G IS
	SELECT
		*
	FROM
		CRST_GL_PERIOD_SETS;

BEGIN

	--  RENAME
	UPDATE
		GL_PERIOD_SETS
	SET
		PERIOD_SET_NAME	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(PERIOD_SET_NAME,1,4),PERIOD_SET_NAME)
	WHERE	PERIOD_SET_NAME	IN	(
								SELECT
									PERIOD_SET_NAME
								FROM
									CRST_GL_PERIOD_SETS
								);

	 FOR C1 IN Cur_F LOOP
	 	BEGIN
			INSERT INTO GL_PERIOD_SETS VALUES
			(
			C1.PERIOD_SET_NAME,
			C1.LAST_UPDATE_DATE,
			C1.LAST_UPDATE_BY,
			C1.CREATION_DATE,
			C1.CREATION_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.DESCRIPTION,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.CONTEXT	
			);
	EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;			
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
ALTER TRIGGER GL_PERIOD_SETS_CT1 ENABLE;
